// pages/findSinger/index.js
const ac = wx.getBackgroundAudioManager()
let lastIndex
let config = {
	offset: 0,
	songCount: 1
}
// let seek = false
Page({
	onHide(){
		wx.setStorageSync('word', this.data.word)
	},
	onUnload(){
		this.onHide()
	},
	onReachBottom(e) {
		this.request(this.data.word)
	},
	search(e) {
		let word = e.detail.value
		console.log(word)
		config.offset = 0
		this.setData({songs:[]})
		this.request(word)
	},
	request(word) {
		if (!(word && config.offset < config.songCount)) return
		wx.request({
			url: "https://music.163.com/api/search/get/?s=" + word + "&limit=20&type=1&offset=" + config.offset,
			success: (e) => {
				console.log("获取的数据：", e)
				let songCount = e.data.result.songCount
				config.songCount = songCount
				let a = e.data.result.songs
				let songs = [];
				config.offset += a.length
				let change = {
					songCount,
					word,
					inputValue: ''
				}
				let size = (this.data.songs || []).length
				for (let i = 0; i < a.length; ++i) {
					let s = a[i];
					change['songs[' + (size + i) + ']'] = {
						id: s.id,
						name: s.name,
						duration: s.duration,
						album: s.album.name,
						length: this.formatDuration(s.duration),
					}
				}
				this.setData(change)
			}
		})
	},
	pause(e) {
		console.log("pause...")
		if (this.data.paused) {
			ac.play()
		} else {
			ac.pause()
		}
		this.setData({
			paused: !this.data.paused
		})
	},
	doplay(index) {
		// seek = false
		let songs = this.data.songs
		let song = songs[index]
		let change = {}
		if (lastIndex != undefined) change["songs[" + lastIndex + "].selected"] = false
		change["songs[" + index + "].selected"] = true
		lastIndex = index
		let id = song.id
		let mp3 = "https://music.163.com/song/media/outer/url?id=" + id + ".mp3"
		ac.src = mp3
		ac.title = song.name
		ac.epname = song.album
		// ac.startTime=220
		ac.play()
		change['paused'] = false
		this.setData(change)
		let q = wx.createSelectorQuery()
		q.select('#s' + index).fields({
				size: true,
				rect: true
			}).selectViewport().fields({
				size: true,
				scrollOffset: true
			})
			.exec(r => {
				let s=r[0],v=r[1],scrollTop=0
				console.log(r)
				console.log(s.top,s.bottom,v.height,v.scrollTop)
				if(s.top<0){
					scrollTop=v.scrollTop+s.top
				}else if(s.bottom>v.height-10){
					scrollTop=v.scrollTop+s.bottom-(v.height-10)
				}else{
					return
				}
				wx.pageScrollTo({scrollTop})
			})
	},
	play(e) {
		let index = e.currentTarget.dataset.songIndex
		this.doplay(index)
		// let index = e.currentTarget.dataset.songIndex
	},

	formatDuration(ms) {
		ms = parseInt(ms / 1000)
		let m = parseInt(ms / 60)
		let s = ms % 60
		return m + "：" + (s < 10 ? '0' : '') + s
	},
	onLoad(options) {
		let next = () => {
			let nextindex = lastIndex + 1
			if (nextindex >= this.data.songs.length) nextindex = 0
			this.doplay(nextindex)
		}
		ac.onError(next)
		ac.onEnded(next)
		// ac.onCanplay(e => {
		// 	if (seek) return
		// 	console.log('onCanpaly', ac.duration)
		// 	seek = true
		// 	ac.seek(ac.duration - 10)
		// })
		let word=wx.getStorageSync('word')
		this.request(word)
	},


})